package com.itheima.vueTest.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.vueTest.dao.UserDao;
import com.itheima.vueTest.pojo.PageBean;
import com.itheima.vueTest.pojo.User;
import com.itheima.vueTest.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserDao userDao;


    @Override
    public Map<String, Object> pageQuery(PageBean pageBean) {
        //给分页插件类注入参数:页码值,和页面数量展示值
        PageHelper.startPage(pageBean.getCurrentPage(),pageBean.getPageSize());

        //按照搜索条件查询所有数据
        List<User> userList = userDao.findAllUser(pageBean.getPageString());

        //System.out.println(userList);

        //System.out.println("集合长度"+userList.size());

        //将条件查询后的数据作为添加到PageInfo对象中
        PageInfo<User> pageInfo = new PageInfo<>(userList);

        //System.out.println(pageInfo.getList());
        //System.out.println(pageInfo.getTotal());

        //添加到map集合中
        Map<String,Object> map=new HashMap<>();
        map.put("userList",pageInfo.getList());//pageInfo.getList为按照页码,页面展示数量,所有数据计算后的list集合数据
        map.put("total",pageInfo.getTotal());//pageInfo.getTotal()为查询出来的总数据,userList集合的长度
        return map;
    }

    @Override
    public void deleteUserById(int id) {
        userDao.deleteUserById(id);
    }
}
